home *** CD-ROM | disk | FTP | other *** search
-
- /*********************************************************************
- ----------------------------------------------------------------------
-
- $VER: LoadDTPicture 10.0
- ©1996 TEK neoscientists
-
- based upon ViewDT.c © by Cloanto Software.
- Slightly modified.
-
- ----------------------------------------------------------------------
- *********************************************************************/
-
- #include <exec/memory.h>
- #include <graphics/gfx.h>
- #include <graphics/displayinfo.h>
- #include <intuition/gadgetclass.h>
- #include <datatypes/datatypes.h>
- #include <datatypes/datatypesclass.h>
- #include <datatypes/pictureclass.h>
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include <proto/graphics.h>
- #include <proto/intuition.h>
- #include <proto/datatypes.h>
- #include <string.h>
- #include <stdio.h>
-
- #include "LoadDTPicture.h"
-
- /*********************************************************************
- ----------------------------------------------------------------------
-
- success = IsDataTypes ( filename,namebuf,nbufsize )
-
- file_name: name of the file to inspect
- name_buff: (optional) buffer to store the file format name
- nbuff_size: size of name_buff
-
- Return value
- TRUE if DataTypes recognized the file as a valid picture file
- FALSE otherwise
-
- ----------------------------------------------------------------------
- *********************************************************************/
-
- BOOL IsDataTypes(UBYTE *file_name, UBYTE *name_buff, LONG nbuff_size)
- {
- struct DataType *dtn;
- struct DataTypeHeader *dth;
- BPTR lock;
- BOOL it_is;
-
- it_is = FALSE;
- if (lock = Lock(file_name, ACCESS_READ))
- {
- /* inspect file */
- if (dtn = ObtainDataTypeA(DTST_FILE, (APTR)lock, NULL))
- {
- dth = dtn->dtn_Header;
- if (dth->dth_GroupID == GID_PICTURE) /* is it a picture? */
- {
- it_is = TRUE;
- if (name_buff)
- {
- strncpy(name_buff, dth->dth_Name, nbuff_size);
- *(name_buff + nbuff_size - 1) = 0; /* safe strncpy() termination */
- }
- }
- ReleaseDataType(dtn);
- }
- UnLock(lock);
- }
- return(it_is);
- }
-
-
- /*********************************************************************
- ----------------------------------------------------------------------
-
- error = GetDataTypesPicture ( filename,pic,bmap_flags )
-
- Parameters
- file_name: name of the file to load
- pic: work structure
- bmap_flags: AllocBitMap() flags (BMF_DISPLAYABLE, BMF_INTERLEAVED etc.)
-
- Return value
- 0 if successful (picture info and data in "pic" structure) or
- error code as from dos.library IoErr() function
-
- ----------------------------------------------------------------------
- *********************************************************************/
-
- LONG GetDataTypesPicture(UBYTE *file_name, struct Picture *pic, ULONG bmap_flags)
- {
- Object *obj;
- struct BitMapHeader *bmh;
- struct BitMap *bmap;
- struct gpLayout layout;
- ULONG *creg, *ctab;
- UBYTE *str;
- LONG ncol, crsize, err;
-
- memset(pic, 0, sizeof(struct Picture)); /* clear pic structure */
- err = 0;
-
- if (obj = NewDTObject(file_name,
- DTA_SourceType, DTST_FILE,
- DTA_GroupID, GID_PICTURE,
- PDTA_Remap, FALSE,
- TAG_DONE)) /* get the picture object */
- {
- if (GetDTAttrs(obj,
- PDTA_ModeID, &pic->display_ID,
- PDTA_BitMapHeader, &bmh,
- TAG_DONE) == 2) /* get the bitmap_header and mode_id */
- {
- pic->bmhd = *bmh;
-
- /*
- query the object about its author, copyright and annotation
- */
- if (GetDTAttrs(obj, DTA_ObjAuthor, &str, TAG_DONE) == 1)
- {
- if (str)
- {
- pic->author_size = strlen(str) + 1;
- if (pic->author = AllocMem(pic->author_size, 0))
- strcpy(pic->author, str);
- }
- }
- if (GetDTAttrs(obj, DTA_ObjCopyright, &str, TAG_DONE) == 1)
- {
- if (str)
- {
- pic->copyright_size = strlen(str) + 1;
- if (pic->copyright = AllocMem(pic->copyright_size, 0))
- strcpy(pic->copyright, str);
- }
- }
- if (GetDTAttrs(obj, DTA_ObjAnnotation, &str, TAG_DONE) == 1)
- {
- if (str)
- {
- pic->annotation_size = strlen(str) + 1;
- if (pic->annotation = AllocMem(pic->annotation_size, 0))
- strcpy(pic->annotation, str);
- }
- }
-
- layout.MethodID = DTM_PROCLAYOUT; /* render the object */
- layout.gpl_GInfo = NULL;
- layout.gpl_Initial = TRUE;
-
- if (DoDTMethodA(obj, NULL, NULL, (Msg)&layout))
- {
- if (GetDTAttrs(obj,
- PDTA_BitMap, &bmap,
- PDTA_CRegs, &creg,
- PDTA_NumColors, &ncol,
- TAG_DONE) == 3) /* get the bitmap and its colors */
- {
- if (bmap != NULL && creg != NULL && ncol != 0)
- {
- crsize = (ncol * 3) * 4;
- pic->palette_entries = ncol;
- pic->palette_size = crsize + (2 * 4); /* LoadRGB32() table requirements */
-
- if (pic->palette = AllocMem(pic->palette_size, 0))
- {
- ctab = pic->palette;
- *ctab++ = (ncol << 16) | 0; /* number of colors and first color to load */
- memcpy(ctab, creg, crsize);
- *(ctab + (crsize / 4)) = 0; /* terminator */
- }
- else err = ERROR_NO_FREE_STORE;
-
- if (pic->bmap = AllocBitMap(pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, pic->bmhd.bmh_Depth, bmap_flags, NULL))
- {
- BltBitMap(bmap, 0,0, pic->bmap, 0,0, pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, 0xC0, 0xFF, NULL);
- WaitBlit();
- }
- else err = ERROR_NO_FREE_STORE;
- }
- else err = ERROR_REQUIRED_ARG_MISSING;
- }
- else err = IoErr();
- }
- else err = IoErr();
- }
- else err = ERROR_REQUIRED_ARG_MISSING;
-
- DisposeDTObject(obj); /* free the object */
- }
- else err = IoErr();
-
- if (err)
- FreePicture(pic);
-
- return(err);
- }
-
-
- /*********************************************************************
- ----------------------------------------------------------------------
-
- FreePicture ( pic )
-
- Parameters
- pic: Picture structure with resources to free
-
- Return value
- none
-
- ----------------------------------------------------------------------
- *********************************************************************/
-
- void FreePicture(struct Picture *pic)
- {
- if (pic->bmap)
- {
- WaitBlit();
- FreeBitMap(pic->bmap);
- }
- if (pic->palette)
- FreeMem(pic->palette, pic->palette_size);
-
- if (pic->author)
- FreeMem(pic->author, pic->author_size);
-
- if (pic->copyright)
- FreeMem(pic->copyright, pic->copyright_size);
-
- if (pic->annotation)
- FreeMem(pic->annotation, pic->annotation_size);
-
- memset(pic, 0, sizeof(struct Picture)); /* clear it all */
- }
-